package dale.web.essay.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import dale.web.common.page.Pager;
import dale.web.essay.model.*;
import dale.web.essay.service.EssayService;

@Controller
@RequestMapping("/essay")
public class EssayGetController {

	@Autowired
	private EssayService service;
	
	@RequestMapping(value="/", method=RequestMethod.GET) 
	public String index() {
		return "redirect:/essay/essaySearch";
	}
	
	@RequestMapping(value="/essaySearch", method=RequestMethod.GET)
	public List<Essay> search(@ModelAttribute Essay item, @RequestParam(required=false) Integer page, Model model) {
		int nowPage = page != null ? page.intValue() : 1;
		int blockSize = 10;
		int blockPage = 5;
		Pager nav = service.page(item, nowPage, blockSize, blockPage);
		model.addAttribute("nav", nav);
		return service.search(item, nowPage, blockSize);
	}

	@RequestMapping(value="/essayDetail", method=RequestMethod.GET)
	public Essay detail(@RequestParam Integer no) {
		return service.detail(no);
	}

	@RequestMapping(value="/essayRemove", method=RequestMethod.GET)
	public String remove(@RequestParam Integer no, RedirectAttributes redirectAttributes) {
		service.remove(no);
		redirectAttributes.addFlashAttribute("msg", "Removed sucessfully.");
		return "redirect:/essay/essaySearch";
	}

	@ModelAttribute("typeItems")
	public EssayType[] getTypeItems() {
		return EssayType.values();
	}

}
